﻿using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace Songhay.Xml
{

    public static partial class XmlUtility
    {

        /// <summary>
        /// Returns a <see cref="System.String"/>
        /// for the transformation of the XSLT document
        /// and the XML document.
        /// </summary>
        /// <param name="xslSet">
        /// The source <see cref="System.Xml.XPath.IXPathNavigable"/> XSL set.
        /// </param>
        /// <param name="navigableSet">
        /// The source <see cref="System.Xml.XPath.IXPathNavigable"/> XML set.
        /// </param>
        public static string GetXslString(IXPathNavigable xslSet, IXPathNavigable navigableSet)
        {
            return XmlUtility.GetXslString(xslSet, null, navigableSet);
        }


        /// <summary>
        /// Returns a <see cref="System.String"/>
        /// for the transformation of the XSLT document
        /// and the XML document.
        /// </summary>
        /// <param name="xslSet">
        /// The source <see cref="System.Xml.XPath.IXPathNavigable"/> XSL set.
        /// </param>
        /// <param name="xslArgs">
        /// The <see cref="System.Xml.Xsl.XsltArgumentList"/>.
        /// </param>
        /// <param name="navigableSet">
        /// The source <see cref="System.Xml.XPath.IXPathNavigable"/> XML set.
        /// </param>
        public static string GetXslString(IXPathNavigable xslSet, XsltArgumentList xslArgs, IXPathNavigable navigableSet)
        {

            XslCompiledTransform xslt = new XslCompiledTransform(false);
            xslt.Load(xslSet);

            string ret = null;

            using(MemoryStream ms = new MemoryStream())
            {
                xslt.Transform(navigableSet, xslArgs, ms);
                ms.Position = 0;

                ret = XmlUtility.GetText(ms);
            }
            return ret;
        }
    }
}
